package org.project.single.controller.back;

import com.github.pagehelper.Page;
import org.project.single.pojo.ro.EnableRo;
import org.project.single.pojo.ro.dict.DictDataListRo;
import org.project.single.pojo.ro.dict.DictTypeListRo;
import org.project.single.pojo.vo.dict.*;
import org.project.single.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.stone.project.baseCommon.pojo.vo.R;
import org.stone.project.baseCommon.service.Insert;
import org.stone.project.baseCommon.service.Update;

import javax.validation.Valid;

/**
 * 后台字典接口
 */
@RestController
@RequestMapping("/back/sysDict")
public class SysDictBackController {
    @Autowired
    private SysDictService sysDictService;

    /**
     * 获取字典类别列表
     * @return
     */
    @GetMapping("type/getDictTypeList")
    public R<Page<DictTypeListVo>> getDictTypeList(DictTypeListRo dictTypeListRo){
        return R.ok(sysDictService.getDictTypeListBack(dictTypeListRo));
    }

    /**
     * 获取字典类别详情
     * @return
     */
    @GetMapping("type/detail/{id}")
    public R<DictTypeDetailVo> getDictTypeDetail(@PathVariable String id){
        return R.ok(sysDictService.getDictTypeDetail(id));
    }

    /**
     * 添加字典类别
     * @return
     */
    @PostMapping("type/addDictType")
    public R addDictType(@RequestBody @Validated(Insert.class) DictTypeEditVo dictTypeEditVo){
        sysDictService.editDictType(dictTypeEditVo);
        return R.ok();
    }

    /**
     * 编辑字典类别
     * @return
     */
    @PutMapping("type/editDictType")
    public R editDictType(@RequestBody @Validated(Update.class) DictTypeEditVo dictTypeEditVo){
        dictTypeEditVo.setCode(null);
        sysDictService.editDictType(dictTypeEditVo);
        return R.ok();
    }


    /**
     * 删除字典类别
     * @return
     */
    @DeleteMapping("type/{id}")
    public R deleteDictType(@PathVariable String id){
        sysDictService.deleteDictType(id);
        return R.ok();
    }

    /**
     * 字典类型启用
     * @param enableRo
     * @return
     */
    @PostMapping("type/enableDictType")
    public R enableDictType(@RequestBody @Valid EnableRo enableRo){
        sysDictService.enableDictType(enableRo);
        return R.ok();
    }









    /**
     * 获取字典数据列表
     * @return
     */
    @GetMapping("data/getDictDataList")
    public R<Page<DictDataListVo>> getDictDataList(DictDataListRo dictDataListRo){
        return R.ok(sysDictService.getDictDataListBack(dictDataListRo));
    }

    /**
     * 获取字典类别数据
     * @return
     */
    @GetMapping("data/detail/{id}")
    public R<DictDataDetailVo> getDictDataDetail(@PathVariable String id){
        return R.ok(sysDictService.getDictDataDetail(id));
    }

    /**
     * 添加字典数据
     * @return
     */
    @PostMapping("data/addDictData")
    public R addDictData(@RequestBody @Validated(Insert.class) DictDataEditVo dictDataEditVo){
        sysDictService.editDictData(dictDataEditVo);
        return R.ok();
    }

    /**
     * 编辑字典数据
     * @return
     */
    @PutMapping("data/editDictData")
    public R editDictData(@RequestBody @Validated(Update.class) DictDataEditVo dictDataEditVo){
        dictDataEditVo.setParentCode(null);
        dictDataEditVo.setKeyCode(null);
        sysDictService.editDictData(dictDataEditVo);
        return R.ok();
    }


    /**
     * 删除字典数据
     * @return
     */
    @DeleteMapping("data/{id}")
    public R deleteDictData(@PathVariable String id){
        sysDictService.deleteDictData(id);
        return R.ok();
    }

    /**
     * 字典数据启用
     * @param enableRo
     * @return
     */
    @PostMapping("data/enableDictData")
    public R enableDictData(@RequestBody @Valid EnableRo enableRo){
        sysDictService.enableDictData(enableRo);
        return R.ok();
    }
}
